package com.sdu.cloudservice.service.impl;

/**
 * @author
 * ZMX,LXF
 */

import com.sdu.cloudservice.dao.UserDao;
import com.sdu.cloudservice.dto.Envelope;
import com.sdu.cloudservice.entity.UserEntity;
import com.sdu.cloudservice.service.UserService;
import com.sdu.cloudservice.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.regex.Pattern;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userdao;
    private UserEntity userentity;

    String pattern = "^[a-zA-Z0-9]{4,16}$";
    @Override
    public Envelope register(String username, String password) {
        if(!Pattern.matches(pattern, username)||password.length()<6||password.length()>20)
            return new Envelope(4,"格式错误",null);
        userentity=userdao.findUserByUsername(username);
        if(userentity!=null)
            return new Envelope(2,"用户名已存在",null);
        else{
            Date date = new java.sql.Date(new java.util.Date().getTime());
            int i = userdao.insertUser(username, PasswordUtil.getMd5(password), date, date);
            if(i==1)
                return new Envelope(userdao.findUserByUsername(username));
            else
                return new Envelope(3,"创建失败",null);
        }
    }

    @Override
    public Envelope login(String username, String password) {
        if(!username.matches(pattern))
            return new Envelope(5,"格式错误",null);
        userentity=userdao.findUserByUsername(username);
        if(userentity==null)
            return new Envelope(1,"用户名或密码错误",null);
        else{
            if(PasswordUtil.getMd5(password).equals(userentity.getPassword())) {
                Date date=new java.sql.Date(new java.util.Date().getTime());//更改为当前时间
                userdao.update(date,username);
                return new Envelope(userentity);
            }

            else
                 return new Envelope(6,"用户名或密码错误",null);
        }
    }
    @Override
    public Envelope verify_password(String username,String password){
        userentity=userdao.findUserByUsername(username);
        if(PasswordUtil.getMd5(password).equals(userentity.getPassword()))
            return new Envelope(0,"密码输入正确",null);
        return new Envelope(7,"用户名与密码不匹配，请重新输入密码",null);
    }

    @Override
    public Envelope change_password(String username,String NewPassword){
        if(NewPassword.length()>=6&&NewPassword.length()<=20){
            String md5_new_password=PasswordUtil.getMd5(NewPassword);
            userdao.update1(username,md5_new_password);
            Envelope envelope=new Envelope(userentity);
            envelope.setMessage("密码修改成功");
            envelope.setCode(0);
            return envelope;
        }
        else if(NewPassword.length()>20)
        return new Envelope(9,"密码过长",null);
        else
        return new Envelope(10,"密码过短",null);
    }

    @Override
    public Envelope addDownload(String username, int code) {
        try{
            if(code == 0){
                userdao.addPicDownload(username);
            }else{
                userdao.addExcelDownload(username);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Envelope.dbError;
        }
        return Envelope.success;
    }

}
